﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;

namespace itChatAdmin
{
    public partial class FormSetting : Form
    {
        public FormSetting()
        {
            InitializeComponent();
        }

        private void button15_Click(object sender, EventArgs e)
        {
            //启动itChat服务器
            //启动本软件
            //连接测试

            Object result = new
            {
                action = "Test",
                location = "Client",
                result = "OK",
                datetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
            };
            String sendString = new JavaScriptSerializer().Serialize(result);
            this.textBox4.Text = "";

            //测试iChat后台GET
            try
            {
                this.textBox4.AppendText("测试itChat后台程序(GET)......" + Environment.NewLine);
                WebClient wc = new WebClient();
                String web = wc.DownloadString("http://127.0.0.1:" + this.textBox3.Text + "/");
                JObject json = JObject.Parse(web);
                this.textBox4.AppendText("GET检测结果：" + Environment.NewLine);
                this.textBox4.AppendText("检测方式:" + json["method"] + Environment.NewLine);
                this.textBox4.AppendText("检测时间:" + json["datetime"] + Environment.NewLine);
                this.textBox4.AppendText("回应位置:" + json["location"] + Environment.NewLine);
                this.textBox4.AppendText("检测结果:" + json["result"] + Environment.NewLine);
                this.textBox4.AppendText(Environment.NewLine);
            }
            catch (Exception ex)
            {
                this.textBox4.AppendText(ex.Message + Environment.NewLine);
            }

            //测试iChat后台POST
            try
            {
                this.textBox4.AppendText("测试itChat后台程序(POST)......" + Environment.NewLine);
                WebClient wc = new WebClient();
                StringBuilder postData = new StringBuilder();
                postData.Append(sendString);
                byte[] sendData = Encoding.GetEncoding("utf-8").GetBytes(postData.ToString());
                wc.Headers.Add("Content-Type", "application/json");
                wc.Headers.Add("ContentLength", sendData.Length.ToString());
                byte[] recData = wc.UploadData("http://127.0.0.1:" + this.textBox3.Text + "/", sendData);

                JObject json = JObject.Parse(Encoding.GetEncoding("utf-8").GetString(recData));
                this.textBox4.AppendText("POST检测结果：" + Environment.NewLine);
                this.textBox4.AppendText("检测方式:" + json["method"] + Environment.NewLine);
                this.textBox4.AppendText("检测时间:" + json["datetime"] + Environment.NewLine);
                this.textBox4.AppendText("回应位置:" + json["location"] + Environment.NewLine);
                this.textBox4.AppendText("检测结果:" + json["result"] + Environment.NewLine);
                this.textBox4.AppendText(Environment.NewLine);

            }
            catch (Exception ex)
            {
                this.textBox4.AppendText(ex.Message + Environment.NewLine);
            }

            //测试管理端口GET
            try
            {
                this.textBox4.AppendText("测试管理软件(GET)......" + Environment.NewLine);
                WebClient wc = new WebClient();
                String web = wc.DownloadString("http://127.0.0.1:" + this.textBox2.Text + "/");
                JObject json = JObject.Parse(web);
                this.textBox4.AppendText("GET检测结果：" + Environment.NewLine);
                this.textBox4.AppendText("检测方式:" + json["method"] + Environment.NewLine);
                this.textBox4.AppendText("检测时间:" + json["datetime"] + Environment.NewLine);
                this.textBox4.AppendText("回应位置:" + json["location"] + Environment.NewLine);
                this.textBox4.AppendText("检测结果:" + json["result"] + Environment.NewLine);
                this.textBox4.AppendText(Environment.NewLine);
            }
            catch (Exception ex)
            {
                this.textBox4.AppendText(ex.Message + Environment.NewLine);
            }

            //测试管理端口POST
            try
            {
                this.textBox4.AppendText("测试管理软件(POST)......" + Environment.NewLine);
                WebClient wc = new WebClient();
                StringBuilder postData = new StringBuilder();
                postData.Append(sendString);
                byte[] sendData = Encoding.GetEncoding("utf-8").GetBytes(postData.ToString());
                wc.Headers.Add("Content-Type", "application/json");
                wc.Headers.Add("ContentLength", sendData.Length.ToString());
                byte[] recData = wc.UploadData("http://127.0.0.1:" + this.textBox2.Text + "/", sendData);

                JObject json = JObject.Parse(Encoding.GetEncoding("utf-8").GetString(recData));
                this.textBox4.AppendText("POST检测结果：" + Environment.NewLine);
                this.textBox4.AppendText("检测方式:" + json["method"] + Environment.NewLine);
                this.textBox4.AppendText("检测时间:" + json["datetime"] + Environment.NewLine);
                this.textBox4.AppendText("回应位置:" + json["location"] + Environment.NewLine);
                this.textBox4.AppendText("检测结果:" + json["result"] + Environment.NewLine);

            }
            catch (Exception ex)
            {
                this.textBox4.AppendText(ex.Message + Environment.NewLine);
            }
        }
    }
}
